package com.ruoyi.lxy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.lxy.domain.bo.TTravelagenBo;
import com.ruoyi.lxy.service.*;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lxy.domain.vo.TRouteVo;
import com.ruoyi.lxy.domain.bo.TRouteBo;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 路线
 *
 * @author ruoyi
 * @date 2023-08-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lxy/route")
public class TRouteController extends BaseController {
    private final ITRouteService iTRouteService;
    private final ITTravelagenService iTTravelagenService;
    private final ITTimeService itTimeService;
    private final ITQuestionnaireService itQuestionnaireService;



    /**
     * 查询路线列表
     */
    @SaCheckPermission("lxy:route:list")
    @GetMapping("/list")
    public TableDataInfo<TRouteVo> list(TRouteBo bo, PageQuery pageQuery) {
        switch(LoginHelper.getLoginUser().getRoles().get(0).getRoleName()) {
            case "管理员":
                return iTRouteService.AdminqueryPageList(bo, pageQuery);
            case "旅行社":
                return iTRouteService.TravelagenqueryPageList(bo, pageQuery,false);
//            return iTRouteService.TravelagenqueryPageList(bo, pageQuery);
            case "教职工":

        }
        return iTRouteService.queryPageList(bo, pageQuery);
    }
    @SaCheckPermission("lxy:route:list")
    @GetMapping("/listBefore")
    public TableDataInfo<TRouteVo> listAllBefore(TRouteBo bo, PageQuery pageQuery) {
        switch(LoginHelper.getLoginUser().getRoles().get(0).getRoleName()) {
            case "旅行社":
                return iTRouteService.TravelagenqueryPageList(bo, pageQuery,true);
        }
        return null;
    }
    /**
     * 查询路线列表-满意度查看-仅管理员具备此功能
     */
    @SaCheckPermission("lxy:route:list")
    @GetMapping("/listgrade")
    public TableDataInfo<TRouteVo> listgrade(TRouteBo bo, PageQuery pageQuery) {
        return iTRouteService.AdminqueryPageListGrade(bo, pageQuery);
    }
    //教职工查找路线
    @SaCheckPermission("lxy:route:list")
    @GetMapping("/userlist")
    public TableDataInfo<TRouteVo> Userlist(TRouteBo bo, PageQuery pageQuery){
        return iTRouteService.UserqueryPageList(bo,pageQuery);
    }

    /**
     * 导出路线列表
     */
    @SaCheckPermission("lxy:route:export")
    @Log(title = "路线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TRouteBo bo, HttpServletResponse response) {
        switch(LoginHelper.getLoginUser().getRoles().get(0).getRoleName()) {
            case "管理员":
                List<TRouteVo> list = iTRouteService.queryList(bo);
                ExcelUtil.exportExcel(list, "路线", TRouteVo.class, response);

            case "旅行社":

            case "教职工":

        }
        List<TRouteVo> list = iTRouteService.queryList(bo);
        ExcelUtil.exportExcel(list, "路线", TRouteVo.class, response);
    }

    /**
     * 获取路线详细信息
     * @param id 主键
     */
    @SaCheckPermission("lxy:route:query")
    @GetMapping("/{id}")
    public R<Map<String,Object>> getInfo(@NotNull(message = "主键不能为空")
                                             @PathVariable(value = "id", required = false) Long id) {

        Map<String,Object> ajax=new HashMap<>();
        ajax.put("travelagen",iTTravelagenService.queryList(new TTravelagenBo()));
        if(ObjectUtil.isNotNull(id)){
            ajax.put("agency",iTRouteService.queryById(id));
//            ajax.put("travelagenId",iTRouteService.queryById(id).getTravelagenId());
        }
        switch(LoginHelper.getLoginUser().getRoles().get(0).getRoleName()) {
            case "管理员":
//                return R.ok(iTRouteService.queryById(id));
            case "旅行社":
            case "教职工":
        }
//        return R.ok(iTRouteService.queryById(id));
        return R.ok(ajax);
    }

    /**
     * 新增路线
     */
    @SaCheckPermission("lxy:route:add")
    @Log(title = "路线", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TRouteBo bo) {
        switch(LoginHelper.getLoginUser().getRoles().get(0).getRoleName()) {
            case "管理员":
                return toAjax(iTRouteService.insertByBo(bo));

            case "旅行社":
                return toAjax(iTRouteService.TravelagenqueryinsertByBo(bo));
            case "教职工":

        }
        return toAjax(iTRouteService.insertByBo(bo));
    }

    /**
     * 新增路线
     */
    @SaCheckPermission("lxy:route:add")
    @Log(title = "路线", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/{ids}")
    public R<Void> addBeforeRoute(@NotEmpty(message = "主键不能为空")
                                      @PathVariable Long[] ids) {
        switch(LoginHelper.getLoginUser().getRoles().get(0).getRoleName()) {
            case "旅行社":
                return toAjax(iTRouteService.TravelagenAddBeforeRoute(ids));
        }
        return R.fail();
    }

    /**
     * 修改路线
     */
    @SaCheckPermission("lxy:route:edit")
    @Log(title = "路线", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TRouteBo bo) {
        switch(LoginHelper.getLoginUser().getRoles().get(0).getRoleName()) {
            case "管理员":
                return toAjax(iTRouteService.updateByBo(bo));
            case "旅行社":
                return toAjax(iTRouteService.TravelagenqueryupdateByBo(bo));
            case "教职工":

        }
        return toAjax(iTRouteService.updateByBo(bo));
    }

    /**
     * 删除路线
     *
     * @param ids 主键串
     */
    @SaCheckPermission("lxy:route:remove")
    @Log(title = "路线", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        String roleName = LoginHelper.getLoginUser().getRoles().get(0).getRoleName();
        for (Long id : ids)
            if (itTimeService.CheckTimeExist(id)){
                return R.warn("对应路线存在时间段无法删除");
            }
        switch (roleName) {
            case "管理员":
                return toAjax(iTRouteService.deleteWithValidByIds(Arrays.asList(ids), true));
            case "旅行社":
                return toAjax(iTRouteService.TravelagenquerydeleteWithValidByIds(Arrays.asList(ids), true));
            case "教职工":


        }
        return toAjax(iTRouteService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
    @SaCheckPermission("lxy:route:edit")
    @Log(title = "路线", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/{ids}")
    public R<Void> submit(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
              return toAjax(iTRouteService.submitWithValidByIds(Arrays.asList(ids), true));
    }
    /**
     * 在已有问卷中添加路线
     */
    @SaCheckPermission("lxy:travelagen:edit")
    @Log(title = "旅行社管理", businessType = BusinessType.GRANT)
    @PutMapping("/addQuestionnaireRoute/{ids}")
    public void addQuestionnaireRoute(@PathVariable Long[] ids, Long questionnaireId) {
        itQuestionnaireService.updateRoute(ids,questionnaireId);
    }
    /**
    满意度结果统计及返回意见建议
     */
    @GetMapping("/satisfaction/{id}")
    public R<TRouteVo> satisfactionResult(@PathVariable("id") Long routeId){
        if (iTRouteService.satisfactionResult(routeId)==null){
            return null;
        }
        return R.ok(iTRouteService.satisfactionResult(routeId));
    }

}
